<style>
  /*#ProductionOrder-edit .layui-table-body {*/
  /*    margin-right: -17px;*/
  /*}*/
  .layui-layer-title {
    display: flex;
    align-items: center;
  }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label">生产计划编号</label>
                  <div class="layui-input-inline mr0">
                    <input name="productionPlanCode" class="layui-input" type="text" placeholder="请输入生产计划编号" />
                  </div>
                </div>
                <div class="layui-inline">
                  <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
              <div class="query-rt"></div>
            </div>
          </div>
        </div>

        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <!-- <button id="ProductionOrderBtnAdd" class="layui-btn icon-btn color-reseda">添加</button> -->
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <table class="layui-table" id="ProductionOrderTable" lay-filter="ProductionOrderTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="ProductionOrderTableBar">
  <a class="layui-btn layui-btn-xs layui-btn" lay-event="detail">详情</a>
  <!-- {{# if(d.status == 2){ }} -->
  <a class="layui-btn layui-btn-xs layui-btn" lay-event="activate">激活</a>

  <!-- {{# } }} -->
  <!-- {{# if(d.status == 2 ||d.status == 5){ }} -->
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">终止</a>

  <!-- {{# } }} -->
<!--  <a class="layui-btn layui-btn-xs layui-btn" lay-event="receive">领料</a>-->
<!--  <a class="layui-btn layui-btn-xs layui-btn" lay-event="reporting">报工</a>-->
  <!-- {{# if(d.status == 5){ }} -->
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="pause">暂停</a>
<!--  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">终止</a>-->
  <!-- {{# } }} -->
  <!-- {{# if(d.status == 6 ){ }} -->
  <a class="layui-btn layui-btn-xs layui-btn" lay-event="restart">重启</a>

  <!-- {{# } }} -->
  <!-- {{# if(d.status == 3){ }} -->

  <!-- {{# } }} -->

</script>

<style>
  #ProductionOrder-form .layui-disabled {
    color: #393d49 !important;
  }
</style>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "util", "table", "admin", "laytpl", "laydate", "tableTreeDj"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var util = layui.util;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laytpl = layui.laytpl;
    var laydate = layui.laydate;
    var tableTree = layui.tableTreeDj;

    // 渲染表格
    var insTb = table.render({
      elem: "#ProductionOrderTable",
      url: config.base + "warehouse/warehouse/productionSchedulingPlanDetail/list",
      toolbar: true,
      defaultToolbar: ["filter"],
      where: { access_token: config.getToken().access_token },
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers" },
          { field: "code", align: "center", title: "制令单编号" },
          { field: "productionPlanCode", align: "center", title: "生产计划编号" },
          { field: "productName", align: "center", title: "生产产品名称" },
          { field: "productSpecCode", align: "center", title: "客户产品料号" },
          { field: "plannedQuantity", align: "center", title: "计划生产数量" },
          { field: "completedQuantity", align: "center", title: "完成数量" },
          { field: "workshopName", align: "center", title: "车间" },
          { field: "productionLineName", align: "center", title: "产线" },
          { field: "productionOrderNumber", align: "center", title: "所属生产订单编号" },
          { field: "remark", align: "center", title: "描述" },
          // 状态1.待审核，2.待激活，3.待领料，4.待报工，5.生产中，6.暂停，7.终止，8.完成
          {
            field: "status",
            align: "center",
            title: "状态",
            templet: function (d) {
              return d.status == 1
                ? "待审核"
                : d.status == 2
                ? "待激活"
                : d.status == 3
                ? "待领料"
                : d.status == 4
                ? "待报工"
                : d.status == 5
                ? "生产中"
                : d.status == 6
                ? "暂停"
                : d.status == 7
                ? "终止"
                : "完成";
            }
          },
          // { field: "creatorName", align: "center", title: "创建人" },
          // { field: "createTime", align: "center", title: "创建时间" },
          { align: "center", toolbar: "#ProductionOrderTableBar", title: "操作", width: 240 }
          //
        ]
      ]
    });



    // 工具条点击事件
    table.on("tool(ProductionOrderTable)", function (obj) {
      var data = obj.data;
      var layEvent = obj.event;
      //详情
      if (layEvent === "detail") {
        admin.req(
                "warehouse/warehouse/productionSchedulingPlan/selectProductionSchedulingPlanDetail",
                {
                  id: data.warehouseProductionSchedulingPlanId
                },
                (res) => {
                  if (res.code === 200) {
                    openFormFun("detail", res.data, data.id);
                  }
                }
        );//激活
      } else if (layEvent === "activate") {
        // 确认是否审核通过
        layer.open({
          title: "激活",
          content: "是否确认激活？",
          btn: ["确认激活", "关闭"],
          yes: function (index) {
            console.log(index);
            admin.req("warehouse/warehouse/productionSchedulingPlanDetail/updateProductionSchedulingPlanDetail", {
              id: data.id,
              status: 3
            }, (res) => {
              if (res.code === 200) {
                layer.msg("激活成功");
                layer.close(index);
                insTb.reload();
              }
            });
          }
        });//领料
      } else if (layEvent === "receive") {
        // 确认是否审核通过
        layer.open({
          title: "激活",
          content: "是否确认激活？",
          btn: ["确认激活", "关闭"],
          yes: function (index) {
            console.log(index);
            admin.req("warehouse/warehouse/productionSchedulingPlanDetail/updateProductionSchedulingPlanDetail", {
              id: data.id,
              status: 3
            }, (res) => {
              if (res.code === 200) {
                layer.msg("激活成功");
                layer.close(index);
                insTb.reload();
              }
            });
          }
        });
        //报工
      } else if (layEvent === "reporting") {
        // 确认是否审核通过
        layer.open({
          title: "激活",
          content: "是否确认激活？",
          btn: ["确认激活", "关闭"],
          yes: function (index) {
            console.log(index);
            admin.req("warehouse/warehouse/productionSchedulingPlanDetail/updateProductionSchedulingPlanDetail", {
              id: data.id,
              status: 3
            }, (res) => {
              if (res.code === 200) {
                layer.msg("激活成功");
                layer.close(index);
                insTb.reload();
              }
            });
          }
        });//重启
      } else if (layEvent === "restart") {
        // 确认是否审核通过
        layer.open({
          title: "激活",
          content: "是否确认重启？",
          btn: ["重启", "关闭"],
          yes: function (index) {
            console.log(index);
            admin.req("warehouse/warehouse/productionSchedulingPlanDetail/updateProductionSchedulingPlanDetail", {
              id: data.id,
              status: 9
            }, (res) => {
              if (res.code === 200) {
                layer.msg("激活成功");
                layer.close(index);
                insTb.reload();
              }
            });
          }
        });//暂停
      } else if (layEvent === "pause") {
        // 确认是否审核通过
        layer.open({
          title: "激活",
          content: "是否确认激活？",
          btn: ["确认激活", "关闭"],
          yes: function (index) {
            console.log(index);
            admin.req("warehouse/warehouse/productionSchedulingPlanDetail/updateProductionSchedulingPlanDetail", {
              id: data.id,
              status: 6
            }, (res) => {
              if (res.code === 200) {
                layer.msg("激活成功");
                layer.close(index);
                insTb.reload();
              }
            });
          }
        });//终止
      } else if (layEvent === "stop") {
        // 确认是否审核通过
        layer.open({
          title: "激活",
          content: "是否确认激活？",
          btn: ["确认激活", "关闭"],
          yes: function (index) {
            console.log(index);
            admin.req("warehouse/warehouse/productionSchedulingPlanDetail/updateProductionSchedulingPlanDetail", {
              id: data.id,
              status: 7
            }, (res) => {
              if (res.code === 200) {
                layer.msg("激活成功");
                layer.close(index);
                insTb.reload();
              }
            });
          }
        });
      }
    });
    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", function (data) {
      insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
    });

    form.render("select");

    // 添加按钮点击事件
    $("#ProductionOrderBtnAdd").click(function () {
      openFormFun("add");
    });

    function openFormFun(eventName, data, productId) {
      admin.formOpen({
        area: "90%",
        offset: "5%",
        title: "配置订单物料信息",
        path: "components/warehouse/product/warehouseProductionSchedulingPlanForm.html",
        success: function () {
          parentData.eventName = eventName;
          parentData.data = data;
          parentData.productId = productId;
        }
      });
    }
  });
</script>
